﻿using System;
using System.Collections.Generic;
using System.Web;
using ElectronicCommerce.Framework.Core;
using ElectronicCommerce.Framework.Web.Core;

namespace ElectronicCommerce.Framework.Web.Mvc
{
    public class CustomRequestContext
    {
        private HttpContext _context = null;
        private static HttpCookieSettingCollection _cookieSettings = SingletonProvider<HttpCookieSettingCollection>.UniqueInstance;
        private static HttpNameValueSettingCollection _nameValueSettings = SingletonProvider<HttpNameValueSettingCollection>.UniqueInstance;

        private HttpCookieManager _cookie = null;
        private HttpNameValueCollectionManager _queryString = null;
        private HttpNameValueCollectionManager _form = null;
        private HttpNameValueCollectionManager _headers = null;

        public CustomRequestContext()
            : this(HttpContext.Current)
        {
        }

        public CustomRequestContext(HttpContext context)
        {
            _context = context;
        }

        /// <summary>
        /// Cookie settings manager
        /// </summary>
        public static HttpCookieSettingCollection CookieSettings
        {
            get { return _cookieSettings; }
        }

        /// <summary>
        /// Request name settings manager
        /// </summary>
        public static HttpNameValueSettingCollection NameValueSettings
        {
            get { return _nameValueSettings; }
        }

        /// <summary>
        /// Http Context (Current)
        /// </summary>
        public HttpContext HttpContext
        {
            get { return _context; }
        }

        /// <summary>
        /// Http cookie manager
        /// </summary>
        public HttpCookieManager Cookie
        {
            get
            {
                if (_cookie == null)
                {
                    _cookie = new HttpCookieManager(_context);
                }
                return _cookie;
            }
        }

        /// <summary>
        /// Http querystring manager
        /// </summary>
        public HttpNameValueCollectionManager QueryString
        {
            get
            {
                if (_queryString == null)
                {
                    _queryString = new HttpNameValueCollectionManager(_context.Request.QueryString);
                }
                return _queryString;
            }
        }

        /// <summary>
        /// Http form value manager
        /// </summary>
        public HttpNameValueCollectionManager Form
        {
            get
            {
                if (_form == null)
                {
                    _form = new HttpNameValueCollectionManager(_context.Request.Form);
                }
                return _form;
            }
        }

        /// <summary>
        /// Http headers manager
        /// </summary>
        public HttpNameValueCollectionManager Headers
        {
            get
            {
                if (_headers == null)
                {
                    _headers = new HttpNameValueCollectionManager(_context.Request.Headers);
                }
                return _headers;
            }
        }

        public string Get(string name)
        {
            return Get<string>(name);
        }

        /// <summary>
        /// Get request input value by <T>, Form (Priority) -> QueryString
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="alias"></param>
        /// <returns></returns>
        public T Get<T>(string name) where T : IConvertible
        {
            T v = Form.Get<T>(name);
            if (default(T) == null)
            {
                if (v == null || string.IsNullOrEmpty(System.Convert.ToString(v)))
                {
                    v = QueryString.Get<T>(name);
                }
            }
            else
            {
                if (default(T).Equals(v))
                {
                    v = QueryString.Get<T>(name);
                }
            }
            return v;
        }


        /// <summary>
        /// Get request input value by List<T>, Form (Priority) -> QueryString
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="alias"></param>
        /// <returns></returns>
        public IList<T> GetList<T>(string name) where T : IConvertible
        {
            IList<T> list = Form.GetList<T>(name);
            if (list.Count == 0)
            {
                list = QueryString.GetList<T>(name);
            }
            return list;
        }

        /// <summary>
        /// Get request input value RAW string, Form (Priority) -> QueryString
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public string GetRaw(string name)
        {
            string v = Form.GetRaw(name);
            if (string.IsNullOrEmpty(v))
            {
                v = QueryString.GetRaw(name);
            }
            return v;
        }
    }
}
